package com.faf.ui;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.dao.impl.StoreDaoImpl;
import com.faf.dao.impl.UserDaoImpl;
import com.faf.tools.PHOUtils;
import com.faf.tools.RSAUtils;

/**
 * Servlet implementation class StoreRegister
 */
@WebServlet("/StoreRegister")
public class StoreRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				//设置中文格式
			response.setContentType("text/html;charset=utf-8"); 
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			//读取register.jsp中的个信息
			String store_number = request.getParameter("store_number");
			String store_pwd = request.getParameter("store_pwd");
			String store_rsa = RSAUtils.publicEncrytype(store_pwd);
			String store_phone = request.getParameter("store_phone");
			String store_Name = request.getParameter("store_Name");
			String store_Address = request.getParameter("store_Address");
			String submit = request.getParameter("submit");
			//跟新用户信息
			StoreDaoImpl store = new StoreDaoImpl();
			//添加用户信息的数据库语句
			String sql_register = "insert into store (storePassword,storeNumber,storePhone,storeName,storeAddress)values(?,?,?,?,?) ";
			String sql_check = "select storeId from store where storeNumber = ? ";
			String json = "";
			try {
				StoreDaoImpl udi = new StoreDaoImpl();
				//如果用户名输入是手机号会提示错误
				if(submit.equals("1")){
					System.out.println("用户名正在存入");
					store.executeUpdate(sql_register,store_rsa,store_number,store_phone,store_Name,store_Address);
					System.out.println("用户名存入成功");
					json = "{\"code\":"+1+"}";
					response.getWriter().print(json);
				}
				else if(!PHOUtils.checkNickName(store_number)){
					System.out.println("手机号不可做作用户名");
					json = "{\"code\":"+-1+"}";
					response.getWriter().print(json);
				}
				else if(store_number.equals(udi.getStoreByNumber(store_number).getStoreNumber())){
					System.out.println("用户名已存在");
					json = "{\"code\":"+0+"}";
					response.getWriter().print(json);
				}
				else if(PHOUtils.checkNickName(store_phone)){
					
					System.out.println("请使用正确的手机号*"+PHOUtils.checkNickName(store_phone));
					json = "{\"code\":"+-2+"}";
					response.getWriter().print(json);
				}
				else if(!PHOUtils.checkNickName(store_phone)){
					json = "{\"code\":"+2+"}";
					response.getWriter().print(json);
				}
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	
	}

}
